package com.sam.compare;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CompareTest {

	@SuppressWarnings("unused")
	public static void main(String[] args) {

		List<PhoneNumber> phoneNumbers = new ArrayList<CompareTest.PhoneNumber>();

		PhoneNumber p1 = new PhoneNumber(871, 6354874);

		PhoneNumber p2 = new PhoneNumber(691, 2507202);
		PhoneNumber p5 = new PhoneNumber(691, 2507203);

		PhoneNumber p3 = new PhoneNumber(879, 6548742);

		PhoneNumber p4 = new PhoneNumber(null, null);
		PhoneNumber p6 = new PhoneNumber(691, 6354874);
		System.out.println(p1.compareTo(p2));
		System.out.println(p1.compareTo(p3));

		phoneNumbers.add(p1);
		phoneNumbers.add(p2);
		phoneNumbers.add(p3);
		phoneNumbers.add(p5);
		phoneNumbers.add(p6);

		// System.out.println(p1.compareTo(p2));

		Collections.sort(phoneNumbers);
		System.out.println(phoneNumbers);
		Collections.sort(phoneNumbers, new Comparator<PhoneNumber>() {

			@Override
			public int compare(PhoneNumber o1, PhoneNumber o2) {
				// TODO Auto-generated method stub

				int lineNumDiffer = o1.lineNum - o2.lineNum;
				if (lineNumDiffer != 0) {
					return lineNumDiffer;
				}

				return o1.areaCode - o2.areaCode;

			}

		});

		System.out.println(phoneNumbers);

		CaseInsenceString s1 = new CaseInsenceString("aVc");
		CaseInsenceString s2 = new CaseInsenceString("AVC");
		CaseInsenceString s3 = new CaseInsenceString("bCA");
		CaseInsenceString s4 = new CaseInsenceString("Bca");
		CaseInsenceString s5 = new CaseInsenceString("ctf");
		CaseInsenceString s6 = new CaseInsenceString("CtF");
		CaseInsenceString s7 = new CaseInsenceString("bAc");
		CaseInsenceString s8 = new CaseInsenceString("bdC");

		List<CaseInsenceString> caseInsenceStrings = new ArrayList<CompareTest.CaseInsenceString>();

		caseInsenceStrings.add(s1);
		caseInsenceStrings.add(s2);
		caseInsenceStrings.add(s3);
		caseInsenceStrings.add(s4);
		caseInsenceStrings.add(s5);
		caseInsenceStrings.add(s6);
		caseInsenceStrings.add(s7);
		caseInsenceStrings.add(s8);

		Collections.sort(caseInsenceStrings);

		System.out.println(caseInsenceStrings);

		// Collections.sort(caseInsenceStrings, String.CASE_INSENSITIVE_ORDER);

	}

	public static class PhoneNumber implements Comparable<PhoneNumber> {

		private Integer areaCode;

		private Integer lineNum;

		public PhoneNumber(Integer areaCode, Integer lineNum) {

			this.areaCode = areaCode;
			this.lineNum = lineNum;
		}

		public PhoneNumber() {

		}

		public Integer getAreaCode() {
			return areaCode;
		}

		public void setAreaCode(Integer areaCode) {
			this.areaCode = areaCode;
		}

		public Integer getLineNum() {
			return lineNum;
		}

		public void setLineNum(Integer lineNum) {
			this.lineNum = lineNum;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result
					+ ((areaCode == null) ? 0 : areaCode.hashCode());
			result = prime * result
					+ ((lineNum == null) ? 0 : lineNum.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (!(obj instanceof PhoneNumber))
				return false;
			PhoneNumber other = (PhoneNumber) obj;
			if (areaCode == null) {
				if (other.areaCode != null)
					return false;
			} else if (!areaCode.equals(other.areaCode))
				return false;
			if (lineNum == null) {
				if (other.lineNum != null)
					return false;
			} else if (!lineNum.equals(other.lineNum))
				return false;
			return true;
		}

		@Override
		public String toString() {
			return "PhoneNumber [areaCode=" + areaCode + ", lineNum=" + lineNum
					+ "]";
		}

		@Override
		public int compareTo(PhoneNumber o) {

			/** 优先排序区域码 **/
			int areaCodeDiffer = this.areaCode - o.areaCode;
			if (areaCodeDiffer != 0) {
				return areaCodeDiffer;
			}
			/** 如果相等则排序lineNum **/
			int lineNumDiffer = this.lineNum - o.lineNum;
			if (lineNumDiffer != 0) {
				return lineNumDiffer;
			}
			return 0;
		}

	}

	public static final class CaseInsenceString implements
			Comparable<CaseInsenceString> {

		private String s;

		public String getS() {
			return s;
		}

		public void setS(String s) {
			this.s = s;
		}

		public CaseInsenceString(String s) {

			this.s = s;
		}

		public CaseInsenceString() {

		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((s == null) ? 0 : s.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (!(obj instanceof CaseInsenceString))
				return false;
			CaseInsenceString other = (CaseInsenceString) obj;
			if (s == null) {
				if (other.s != null)
					return false;
			} else if (!s.equals(other.s))
				return false;
			return true;
		}

		@Override
		public String toString() {
			return "CaseInsenceString [s=" + s + "]";
		}

		@Override
		public int compareTo(CaseInsenceString o) {

			return String.CASE_INSENSITIVE_ORDER.compare(this.s, o.getS());
		}

	}
}
